<script lang="ts" setup name="XtxCity">
import { ref, watch } from 'vue';
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'
// 城市列表类型
export type AreaList = {
  code: string
  level: number
  name: string
  areaList: AreaList[]
}
// 选择的城市结果类型
export type CityResult = {
  provinceCode: string
  provinceName: string
  cityCode: string
  cityName: string
  countyCode: string
  countyName: string
}
//父传子
defineProps<{
  userAddress?: string
}>()
const emit = defineEmits<{
  (e:'changeCity',value:CityResult):void
}>()
//点击高亮和盒子显示隐藏
const active=ref(false)
const toggle = ()=>{
  active.value = !active.value
}
//点击外部,隐藏盒子
const target = ref(null)
onClickOutside(target, () => {
  active.value = false
})
//存城市数据
const cityList = ref<AreaList[]>([])
const cacheCityList =ref<AreaList[]>([])
//发请求函数,获取到城市数据
const getCityList = async()=>{
  const res = await axios.get<AreaList[]>(
   'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
  cityList.value = res.data
  cacheCityList.value = res.data
  // console.log(cityList.value);
}
getCityList()
// 选择城市数据存储
const changeResult = ref({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: ''
})
//选择城市
const selectCity =(city: AreaList)=>{
  if(city.level === 0){
    //省
    changeResult.value.provinceCode = city.code
    changeResult.value.provinceName = city.name
    cityList.value = city.areaList   //覆盖上一级
  }
   if(city.level === 1){
    //市
    changeResult.value.cityCode = city.code
    changeResult.value.cityName = city.name
    cityList.value = city.areaList
  }
  if(city.level === 2){
    //区县
    changeResult.value.countyCode = city.code
    changeResult.value.countyName = city.name
    // 关闭弹窗
    active.value = false
     // 子传父
    emit('changeCity', changeResult.value)
  }
}
//监听盒子显示隐藏
watch(active,(value)=>{
  if(!value)[
    //盒子关闭，恢复数据
     cityList.value = cacheCityList.value,
     //
  ]
})
</script>


<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{active:active}">
      <span class="placeholder" v-if="!changeResult.countyName" >请选择配送地址</span>
      <span class="value" v-else>
        {{userAddress}}
        </span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <span class="ellipsis" 
      @click="selectCity(item)"
      v-for="item in cityList" 
      :key="item.code">{{item.name}}</span>
    </div>
  </div>
</template>



<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>